/**
 * <pre>
 * Title: 		ButtonTag.java
 * Type:		com.hengpeng.common.web.tags.ButtonTag
 * Author:		linriqing
 * Create:	 	2011-7-10 下午06:00:29
 * Copyright: 	Copyright (c) 2011
 * Company:
 * <pre>
 */
package com.hengpeng.common.web.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.hengpeng.common.acl.impl.PermissionProviderImpl;
import com.hengpeng.common.acl.vo.ButtonObject;
import com.hengpeng.common.acl.vo.Passport;
import com.hengpeng.common.acl.vo.Permission;
import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.SystemException;
import com.hengpeng.common.exception.TransException;

/**
 * <pre>
 * 页面按钮标签
 * </pre>
 * @author linriqing
 * @version 1.0, 2011-7-10
 */
public class ButtonTag extends BodyTagSupport
{
	private static final long serialVersionUID = 5961409054311151830L;

	private String description;

	private String destination;

	private String event;

	private String image;
	private String showText;

	public ButtonTag()
	{
		super();
	}

	public int doEndTag() throws JspException
	{
		return super.doEndTag();
	}

	public int doStartTag() throws JspException
	{
		super.doStartTag();
		ButtonContainerTag parentButtonTag = (ButtonContainerTag) this.getParent();
		Permission permission = PermissionProviderImpl.getInstance().getPermissionByAction(this.destination);
		if (isButtonShow(permission))
		{
			ButtonObject e = new ButtonObject();
			e.setDescription(description);
			e.setDestination(destination);
			e.setImage(image);
			e.setShowText(showText);
			e.setEvent(event);
			parentButtonTag.getButtons().add(e);
		}
		return SKIP_BODY;
	}

	/**
	 * @return description
	 */
	public String getDescription()
	{
		return description;
	}

	/**
	 * @return destination
	 */
	public String getDestination()
	{
		return destination;
	}

	/**
	 * @return event 
	 */
	public String getEvent()
	{
		return event;
	}

	public String getImage()
	{
		return image;
	}

	/**
	 * @return name
	 */
	public String getShowText()
	{
		return showText;
	}

	public boolean isButtonShow(Permission permission) throws JspException
	{
		Passport passport = (Passport) this.pageContext.getSession().getAttribute(Constant.PROFILE_SESSION_NAME);

		if (passport == null)
		{
			return false;
		}
		else
		{
			try
			{
				return PermissionProviderImpl.getInstance().validPermission(passport, permission);
			}
			catch (TransException e)
			{
				throw new SystemException(e);
			}
		}
	}

	/**
	 * @param description description
	 */
	public void setDescription(String description)
	{
		this.description = description;
	}

	/**
	 * @param destination destination
	 */
	public void setDestination(String destination)
	{
		this.destination = destination;
	}

	/**
	 * @param event event
	 */
	public void setEvent(String event)
	{
		this.event = event;
	}

	public void setImage(String image)
	{
		this.image = image;
	}

	/**
	 * @param name name
	 */
	public void setShowText(String name)
	{
		this.showText = name;
	}
}
